//
// Created by hjie on 23-7-8.
//

#include "link.h"
#include <vector>
#include <algorithm>

void NoSortLinkList(LinkList & list, int length, int min, int max)
{
    srand(time(NULL) + rand() % 1000);
    for (int i = 0; i < length; i++)
    {
        int num = rand() % (max - min) + min;
        LinkNode * node = new LinkNode;
        node->data = num;
        node->next_node = nullptr;
        node->prev_node = nullptr;

        if (list.head_node == nullptr)
        {
            list.head_node = node;
            list.tail_node = node;
        }
        else
        {
            list.tail_node->next_node = node;
            node->prev_node = list.tail_node;
            list.tail_node = node;
        }
    }
}

void SortLinkList(LinkList & list, int length, int min, int max)
{
    srand(time(NULL) + rand() % 1000);
    std::vector<int> number_vec(length);
    for (int i = 0; i < length; i++)
    {
        int num = rand() % (max - min) + min;
        number_vec[i] = num;
    }
    std::sort(number_vec.begin(), number_vec.end());

    list.head_node = nullptr;
    list.tail_node = nullptr;

    for (auto & data : number_vec)
    {
        LinkNode * node = new LinkNode;
        node->data = data;
        node->next_node = nullptr;
        node->prev_node = nullptr;

        if (list.head_node == nullptr)
        {
            list.head_node = node;
            list.tail_node = node;
        }
        else
        {
            list.tail_node->next_node = node;
            node->prev_node = list.tail_node;
            list.tail_node = node;
        }
    }
}

void PrintLinkList(LinkList & list)
{
    LinkNode * ptr = list.head_node;
    printf("%s : ", __func__);
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf(".\n");
}